package tech.windanchaos.blog.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import tech.windanchaos.blog.entity.ArticleDes;
import tech.windanchaos.blog.service.ArticleService;

import java.util.List;

/**
 * 普通用不访问博客的controller
 */
@Controller
class UserController {
    @Autowired
    ArticleService articleService;

    /**
     * 首页没学会如何用pageHelper来处理，暂时直接写死
     * @param model
     * @return
     */
    @Transactional
    @GetMapping({"/","index","blog"})
    public String homePage(@RequestParam(name = "page", required = false, defaultValue = "1") int page,
                           @RequestParam(name = "size", required = false, defaultValue = "6") int size,
                           Model model) {
        //防止传参过大强制纠正
        if (size > 10) size = 10;
        List<ArticleDes> articleDesList = articleService.getArticlesByPage((page - 1) * size, size);
        //自己维护pageinfo中的个字段的值，页面通过调取pageinfo的方法来行为
        PageInfo pageInfo = new PageInfo(articleDesList);
        pageInfo.setNextPage(page + 1);
        pageInfo.setPrePage(page - 1);
        //控制上一页和下一页显示与否的办法，是第一页不显示上一页，是最后一页，不显示下一页
        if (size > articleDesList.size()) {
            pageInfo.setIsLastPage(true);
        } else pageInfo.setIsLastPage(false);
        //取得的数据没有size那么长说明最后一页了。刚好最后一页是长度和size一样无法判定
        if (size > articleDesList.size()) {
            pageInfo.setIsLastPage(true);
        }
        if (pageInfo.getPrePage() <= 0) {
            pageInfo.setIsFirstPage(true);
        } else {
            pageInfo.setIsFirstPage(false);
        }
        //articleService.getCategoriesByArticleID()
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("categories",articleService);
        return "index";
    }


    /**
     * 请求参数可以用ModelAttribute注解
     * @param
     * @return
     */

    @GetMapping("/archive")
    public String archive(@RequestParam(name = "page", required = false, defaultValue = "1") int page,
                          @RequestParam(name = "size", required = false, defaultValue = "16") int size,Model model
    ){
        List<ArticleDes> articleDesList = articleService.getArticlesByPage((page - 1) * size, size);
        PageInfo pageInfo = new PageInfo(articleDesList);
        pageInfo.setNextPage(page + 1);
        pageInfo.setPrePage(page - 1);
        //控制上一页和下一页显示与否的办法，是第一页不显示上一页，是最后一页，不显示下一页
        if (size > articleDesList.size()) {
            pageInfo.setIsLastPage(true);
        } else pageInfo.setIsLastPage(false);
        //取得的数据没有size那么长说明最后一页了。刚好最后一页是长度和size一样无法判定
        if (size > articleDesList.size()) {
            pageInfo.setIsLastPage(true);
        }
        if (pageInfo.getPrePage() <= 0) {
            pageInfo.setIsFirstPage(true);
        } else {
            pageInfo.setIsFirstPage(false);
        }
        model.addAttribute("pageInfo",pageInfo);
        return "archive";
    }

    /**
     * 博客正文
     * @return
     */
    @GetMapping("/blog/{id}")
    public String getBlogByID(Model model, @PathVariable Integer id){
        model.addAttribute("blog",articleService.getArticleByID(id));
        model.addAttribute("categories",articleService);
        return "blog";
    }

    /**
     * 分类页
     * @param model
     * @return
     */
    @GetMapping("/category")
    public String category(Model model){
        model.addAttribute("categoryList",articleService.getArticleCategoryList());
        model.addAttribute("category",null);
        return "category";
    }


    @GetMapping("/category/{id}")
    public String category(Model model, @PathVariable Integer id){
        model.addAttribute("categoryList",null);
        model.addAttribute("blogDesList",articleService.getArticleDesByCategoryID(id));
        model.addAttribute("category",articleService.getCategoryByID(id));
        return "category";
    }

    /**
     * 标签页
     * @param model
     * @return
     */
    @GetMapping("/tags")
    public String tags(Model model){
        model.addAttribute("tagList",articleService.getArticleTagList());
        model.addAttribute("tag",null);
        return "tags";
    }
    @GetMapping("/tags/{id}")
    public String tags(Model model, @PathVariable Integer id){
        model.addAttribute("tagList",null);
        model.addAttribute("blogDesList",articleService.getArticleDesByTagID(id));
        model.addAttribute("tag",articleService.getTagByID(id));
        return "tags";
    }
}
